package myListener;

import java.awt.Color;
import java.awt.FlowLayout;

import javax.swing.JList;

import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import myWindows.JMyVentana;

public class JTestList extends JMyVentana {
    public JTestList() {
        InitGUI();
    }
    
    private JList lista;
    private final String nombreColores[] = {
            "Negro", "Azul", "Cyan", "Gris Oscuro", "Gris", "Verde", "Gris Claro",
            "Magenta", "Naranja", "Rosa", "Rojo", "Blanco", "Amarillo"};
            
    private final Color colores[] = {
            Color.BLACK, Color.BLUE, Color.CYAN, Color.DARK_GRAY, Color.GRAY, 
            Color.GREEN, Color.LIGHT_GRAY,
            Color.MAGENTA, Color.ORANGE, Color.PINK, Color.RED, Color.WHITE, 
            Color.YELLOW};

    
    public void InitGUI(){
        this.getContentPane().setLayout(new FlowLayout());    
        this.setSize(250, 300);
        
        lista = new JList(nombreColores);
        //Fijar el numero de items a mostrar 6
        lista.setVisibleRowCount(6);
        
        //no permitir seleccion multiple
        lista.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        
        //Para tener acceso a un Vertical ScrollBar
        add(new JScrollPane(lista));
        
        //Adicionar el listener para capturar la seleccion de un item
        lista.addListSelectionListener(new ListSelectionListener(){
                    public void valueChanged(ListSelectionEvent e) {
                        getContentPane().setBackground(
                            colores[lista.getSelectedIndex()]);
                    }
                });
    }

    public static void main(String[] args) {
        JTestList obj = new JTestList();
        obj.setVisible(true);
    }
}
